package com.example.pv.alarmservice;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.view.View.OnClickListener;

import java.util.Calendar;

public class alarmService extends AppCompatActivity {

    private static alarmService appRef = null;
    private Button bt1, bt2;
    boolean k = false;

    public void onCreate(Bundle bundle){
        super.onCreate(bundle);
        appRef=this;
        setContentView(R.layout.alarm_service);
        bt1=(Button)findViewById(R.id.button);
        bt2=(Button)findViewById(R.id.button2);
        bt1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                setTitle("Waiting... Alarm=5");
                Intent intent=new Intent(alarmService.this,alarmReceiver.class);
                PendingIntent p=PendingIntent.getBroadcast(alarmService.this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
                Calendar calendar =Calendar.getInstance();
                calendar.setTimeInMillis(System.currentTimeMillis());
                calendar.add(Calendar.SECOND, 3);
                AlarmManager am=(AlarmManager) getSystemService(ALARM_SERVICE);
                am.set(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),p);
            }
        });
        bt2.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent =new Intent(alarmService.this,alarmReceiver.class);
                PendingIntent p =PendingIntent.getBroadcast(alarmService.this, 0 ,intent, 0);
                AlarmManager am =(AlarmManager)getSystemService(ALARM_SERVICE);
                am.cancel(p);
                finish();

            }
        });
    }

    public static alarmService getApp(){
        return appRef;
    }

    public void btEvent(String data){
        setTitle(data);
    }
}
